package com.wb.mas.ui.login;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.SPUtils;
import com.wb.mas.R;
import com.wb.mas.app.SPKeys;
import com.wb.mas.data.DemoRepository;
import com.wb.mas.entity.LoginResponse;
import com.wb.mas.ui.main.MainActivity;
import com.wb.mas.ui.main.MainViewModel;
import com.wb.mas.utils.response.ResultException;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * Copyright (C) 2018
 * LoginAccountModel
 * <p>
 * Description
 *
 * @author mwl
 * @version 1.0
 * <p>
 * Ver 1.0, 2019/5/8 0008, mwl, Create file
 */
public class LoginAccountModel extends BaseViewModel<DemoRepository> {
    /**
     * 用户名称绑定
     **/
    public ObservableField<String> userName = new ObservableField<>();

    /**
     * mima
     **/
    public ObservableField<String> password = new ObservableField<>();


    public BindingCommand clickLogin = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (TextUtils.isEmpty(userName.get())) {
                ToastUtils.showShort(getString(R.string.login_user_name_not_null));
                return;
            }
            if (TextUtils.isEmpty(password.get())) {
                ToastUtils.showShort(getString(R.string.login_password_not_null));
                return;
            }
            showDialog();
            Map<String, Object> parms = new HashMap<>();
            parms.put("userName", userName.get());
            parms.put("passWord", password.get());
            model.loginByUserNameAndPassword(parms)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<LoginResponse>() {
                        @Override
                        public void accept(LoginResponse loginResponse) throws Exception {
                            dismissDialog();
                            if (loginResponse == null || loginResponse.getData() == null) return;
                            String token = loginResponse.getData().getToken();
                            SPUtils.getInstance().put(SPKeys.KEY_OF_TOKEN, token);

                            Messenger.getDefault().send("", MainViewModel.AUTH_SUC_EVENT);

                            startActivity(MainActivity.class);
                            finish();
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            dismissDialog();
                            if (throwable instanceof ResultException) {
                                ToastUtils.showShort(throwable.getMessage());

                            }
                            KLog.e("login failed:" + throwable.getMessage());

                        }
                    });
        }
    });

    public LoginAccountModel(@NonNull Application application, DemoRepository model) {
        super(application, model);
    }
}
